package tx.bj12320.action;

import java.io.File;
import java.util.Hashtable;

import javacommon.base.BaseStruts2Action;

import org.apache.struts2.ServletActionContext;

import tx.bj12320.model.Resource;
import tx.bj12320.service.ResourceManager;
import tx.bj12320.util.WebUtils;
import tx.bj12320.vo.query.ResourceQuery;
import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.web.scope.Flash;
import cn.org.rapid_framework.web.util.HttpUtils;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ResourceAction extends BaseStruts2Action implements Preparable,
		ModelDriven {
	// 默认多列排序,example: username desc,createTime asc
	public static final String DEFAULT_SORT_COLUMNS = "t.resourceIssueDate desc  ";

	// forward paths
	protected static final String QUERY_JSP = "/pages/Resource/query.jsp";
	protected static final String LIST_JSP = "/pages/Resource/list.jsp";
	protected static final String CREATE_JSP = "/pages/Resource/create.jsp";
	protected static final String EDIT_JSP = "/pages/Resource/edit.jsp";
	protected static final String SHOW_JSP = "/pages/Resource/show.jsp";
	protected static final String VIEW_JSP = "/resource-for-view.jsp";
	// redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/Resource/list.do";

	// 新增普通用户浏览
	protected static final String USER_LIST_JSP = "/resource-for-browse.jsp";

	private ResourceManager resourceManager;

	private Resource resource;
	java.lang.Long id = null;
	private String[] items;

	private File uploadresourceFile;
	private String uploadresourceFileContentType;
	private String uploadresourceFileFileName;

	public File getUploadresourceFile() {
		return uploadresourceFile;
	}

	public void setUploadresourceFile(File uploadresourceFile) {
		this.uploadresourceFile = uploadresourceFile;
	}

	public String getUploadresourceFileContentType() {
		return uploadresourceFileContentType;
	}

	public void setUploadresourceFileContentType(
			String uploadresourceFileContentType) {
		this.uploadresourceFileContentType = uploadresourceFileContentType;
	}

	public String getUploadresourceFileFileName() {
		return uploadresourceFileFileName;
	}

	public void setUploadresourceFileFileName(String uploadresourceFileFileName) {
		this.uploadresourceFileFileName = uploadresourceFileFileName;
	}

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			resource = new Resource();
		} else {
			resource = (Resource) resourceManager.getById(id);
		}
	}

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setResourceManager(ResourceManager manager) {
		this.resourceManager = manager;
	}

	public Object getModel() {
		return resource;
	}

	public void setResourceId(java.lang.Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/** 执行搜索 */
//	public String list() {
//		ResourceQuery query = newQuery(ResourceQuery.class,
//				DEFAULT_SORT_COLUMNS);
//		query.setIsResourceType(10);//　tmpl
//		Page page = resourceManager.findPage(query);
//		savePage(page, query);
//		return LIST_JSP;
//	}

	/* 普通用户执行搜索 */
//	public String role_report_unit_list() {
//		ResourceQuery query = newQuery(ResourceQuery.class,
//				DEFAULT_SORT_COLUMNS);
//		query.setIsResourceType(10);//
//		Page page = resourceManager.findPage(query);
//		savePage(page, query);
//		return USER_LIST_JSP;
//	}

	/** 查看对象 */
	public String show() {
		return SHOW_JSP;
	}

	public String view() {
		return VIEW_JSP;
	}

	/** 进入新增页面 */
	public String create() {
		return CREATE_JSP;
	}

	/** 保存新增对象 */
	public String save() {
//		if (WebUtils.isUserRole()) {
//			resource.setBjmdrUserId(WebUtils.getUserInSession()
//					.getBjmdrUserId());
//		}

		resourceManager.save(resource);
		if (uploadresourceFile != null) {
			String fileFormat = WebUtils
					.getExtention(uploadresourceFileFileName);
			String newFileName = resource.getResourceId() + "_" + "rs"
					+ fileFormat;
			File newFile = new File(ServletActionContext.getServletContext()
					.getRealPath("/resource-files")
					+ "/" + newFileName);
			WebUtils.copy(uploadresourceFile, newFile);

			resource.setResourceFile(newFileName);
		}
		resourceManager.saveOrUpdate(resource);
		Flash.current().success(CREATED_SUCCESS); // 存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return LIST_ACTION;
	}

	/** 进入更新页面 */
	public String edit() {
		return EDIT_JSP;
	}

	/** 保存更新对象 */
	public String update() {
		if (uploadresourceFile != null) {
			String fileFormat = WebUtils
					.getExtention(uploadresourceFileFileName);
			String newFileName = resource.getResourceId() + "_" + "rs"
					+ fileFormat;
			File newFile = new File(ServletActionContext.getServletContext()
					.getRealPath("/resource-files")
					+ "/" + newFileName);
			WebUtils.copy(uploadresourceFile, newFile);

			resource.setResourceFile(newFileName);
		}

		resourceManager.update(this.resource);
		Flash.current().success(UPDATE_SUCCESS);
		return LIST_ACTION;
	}

	/** 删除对象 */
	public String delete() {
		for (int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			java.lang.Long id = new java.lang.Long((String) params
					.get("resourceId"));
			resourceManager.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}

	/** 常用网 */
	public String queryUsuallyURLForBrowse() {
		ResourceQuery query = newQuery(ResourceQuery.class,
				DEFAULT_SORT_COLUMNS);
		query.setResourceType(new Long(11));// 常用网址类型为11
		Page page = resourceManager.findPage(query);
		savePage(page, query);
		return "/query-usually-URL-for-browse.jsp";
	}

	public String usuallyURLToAdd() {

		return "/usually-URL-add.jsp";
	}

	public String usuallyURLAdd() {
		resource.setUserInfoId(WebUtils.getUserInSession().getUserInfoId());
		resource.setResourceType(new Long(11));
		resourceManager.save(resource);
		Flash.current().success(CREATED_SUCCESS);
		return "!/pages/Resource/queryUsuallyURLForBrowse.do";
	}

	public String usuallyURLToModify() {

		return "/usually-URL-modify.jsp";
	}

	public String usuallyURLModify() {
		resource.setUserInfoId(WebUtils.getUserInSession().getUserInfoId());

		resourceManager.update(this.resource);
		Flash.current().success(UPDATE_SUCCESS);
		return "!/pages/Resource/queryUsuallyURLForBrowse.do";
	}

	/**删除对象*/
	public String usuallyURLDelete() {
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			java.lang.Long id = new java.lang.Long((String)params.get("resourceId"));
			resourceManager.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return "!/pages/Resource/queryUsuallyURLForBrowse.do";
	}



	/** 常用模版 */
//	public String queryUsuallyTemplateForBrowse() {
//		ResourceQuery query = newQuery(ResourceQuery.class,
//				DEFAULT_SORT_COLUMNS);
//		query.setResourceType(10);// 常用模版10
//		Page page = resourceManager.findPage(query);
//		savePage(page, query);
//		return "/query-usually-template-for-browse.jsp";
//	}

	public String usuallyTemplateToAdd() {

		return "/usually-template-add.jsp";
	}

//	public String usuallyTemplateAdd() {
//		resource.setUserInfoId(WebUtils.getUserInSession().getUserInfoId());
//		resource.setResourceType(10);
//		resourceManager.save(resource);
//		if (uploadresourceFile != null) {
//			String fileFormat = WebUtils
//					.getExtention(uploadresourceFileFileName);
//			String newFileName = resource.getResourceId() + "_" + "rs"
//					+ fileFormat;
//			File newFile = new File(ServletActionContext.getServletContext()
//					.getRealPath("/resource-files")
//					+ "/" + newFileName);
//			WebUtils.copy(uploadresourceFile, newFile);
//
//			resource.setResourceFile(newFileName);
//		}
//		resourceManager.saveOrUpdate(resource);
//		Flash.current().success(CREATED_SUCCESS);
//		return "!/pages/Resource/queryUsuallyTemplateForBrowse.do";
//	}

	public String usuallyTemplateToModify() {

		return "/usually-template-modify.jsp";
	}

//	public String usuallyTemplateModify() {
//		if (WebUtils.isUserRole()) {
//			resource.setBjmdrUserId(WebUtils.getUserInSession()
//					.getBjmdrUserId());
//		}
//		if (uploadresourceFile != null) {
//			String fileFormat = WebUtils
//					.getExtention(uploadresourceFileFileName);
//			String newFileName = resource.getResourceId() + "_" + "rs"
//					+ fileFormat;
//			File newFile = new File(ServletActionContext.getServletContext()
//					.getRealPath("/resource-files")
//					+ "/" + newFileName);
//			WebUtils.copy(uploadresourceFile, newFile);
//
//			resource.setResourceFile(newFileName);
//		}
//		resourceManager.update(this.resource);
//		Flash.current().success(UPDATE_SUCCESS);
//		return "!/pages/Resource/queryUsuallyTemplateForBrowse.do";
//	}
	/**删除对象*/
//	public String usuallyTemplateDelete() {
//		for(int i = 0; i < items.length; i++) {
//			Hashtable params = HttpUtils.parseQueryString(items[i]);
//			java.lang.Integer id = new java.lang.Integer((String)params.get("resourceId"));
//			resourceManager.removeById(id);
//		}
//		Flash.current().success(DELETE_SUCCESS);
//		return "!/pages/Resource/queryUsuallyTemplateForBrowse.do";
//	}

}
